import {
  queryClassListByTeacher,
  queryTeacherThesisList,
  updateThesisStatus,
} from '@/services/ThesisManage/thesisReview/ThesisController';
import { HighlightOutlined, RedoOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import { Button, message } from 'antd';
import { useEffect, useRef, useState } from 'react';
import { history } from 'umi';
import { baseColumns } from './constants/constants';

const MyThesis = () => {
  // table form逻辑
  const modalFormRef = useRef(null);
  const tableRef = useRef(null);
  const [columns, setColumns] = useState(baseColumns);

  useEffect(() => {
    updateColumns();
  }, []);

  const updateColumns = async () => {
    const res = await queryClassListByTeacher();
    if (res.code === 200 && res.data?.class?.length) {
      const newColums = [...columns];
      newColums[3].valueEnum = new Map(
        res.data.class.map((value) => {
          return [value.class_id, value.class_name];
        }),
      );
      newColums.splice(11, 1, {
        title: '操作',
        key: 'option',
        valueType: 'option',
        width: 200,
        render: (_, value) => [
          <Button
            type="primary"
            title="审查"
            key="edit"
            shape="circle"
            icon={<HighlightOutlined />}
            onClick={() => toReview(value)}
          />,
          value.thesis_status !== '待审查' ? (
            <Button
              type="primary"
              title="打回"
              key="back"
              shape="circle"
              icon={<RedoOutlined />}
              onClick={() => repulse(value)}
            />
          ) : null,
        ],
      });
      setColumns(newColums);
    }
  };

  const toReview = async (value) => {
    if (value.thesis_status === '待审查') {
      await updateThesisStatus({
        thesis_id: value.thesis_id,
        thesis_status: '审查中',
      });
    }
    history.push(`/thesisManage/teacherThesis/detail/${value.thesis_id}`);
  };

  const repulse = async (value) => {
    const res = await updateThesisStatus({
      thesis_id: value.thesis_id,
      thesis_status: '待审查',
    });
    if (res.code === 200) {
      tableRef?.current?.reload();
    }
  };

  const onDel = async (id) => {
    const res = await delUser({ id });
    if (res?.code === 200) {
      message.success(res?.message || '删除成功');
      tableRef?.current?.reloadAndRest?.();
    }
  };

  return (
    <PageContainer>
      <ProTable
        actionRef={tableRef}
        bordered
        rowKey="id"
        columns={columns}
        search={{ labelWidth: 'auto' }}
        request={async (params = {}, sort, filter) => {
          const { current, pageSize, ...otherParams } = params;
          const res = await queryTeacherThesisList({
            current,
            pageSize,
            ...otherParams,
          });
          if (res?.code === 200) {
            return {
              total: res?.data?.count,
              data: res?.data?.rows,
              success: true,
            };
          }
          return {
            success: false,
          };
        }}
        pagination={{
          pageSize: 10,
          showSizeChanger: true,
        }}
      />
    </PageContainer>
  );
};

export default MyThesis;
